package com.cloudkinto.controller.firstvesselorder;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.fbaproduct.FbaProductService;
import com.cloudkinto.service.firstvesselorder.FirstVesselOrderService;
import com.cloudkinto.service.firstvesselorder.vo.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author zhangyy
 * @since 2020-09-16
 */
@RestController
@Api(tags = "海运订单程模块")
@RequestMapping("/api/admin/seaTransportation")
public class FirstVesselOrderController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FirstVesselOrderController.class);

    @Autowired
    private FirstVesselOrderService service;
    @Autowired
    private FbaProductService fbaProductService;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated FirstVesselOrderAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        req.setStatus(DicBusinessItemConstant.seaSubmit);
        return SingleResult.success("新增成功",service.add(req,userId,companyId));
    }

//    @UserSessionRequired
//    @PostMapping("draftsSave")
//    @ApiOperation(value = "草稿箱保存", notes = "新增")
//    public SingleResult draftsSave(@RequestBody @Validated FirstVesselOrderAddReq req) {
//        Long userId = getCurrentUserId();
//        Long companyId = getCompanyId();
//        req.setStatus(DicBusinessItemConstant.seaUnsubmit);
//        return SingleResult.success("新增成功",service.add(req,userId,companyId));
//    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @GetMapping("/orderProductDetail/{id}")
    @ApiOperation(value = "订单商品详情", notes = "编辑初始化")
    public SingleResult orderProductDetail(@PathVariable("id") Long id) {
        return service.orderProductDetail(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated FirstVesselOrderUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId));
    }

    @UserSessionRequired
    @PostMapping("updateStatus")
    @ApiOperation(value = "编辑状态", notes = "编辑状态")
    public SingleResult updateStatus(@RequestBody @Validated FirstVesselUpdateStatusReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.updateStatus(req,userId);
        return SingleResult.success();
    }


    @UserSessionRequired
    @GetMapping("updateStatusInit")
    @ApiOperation(value = "编辑状态初始化", notes = "编辑状态")
    public SingleResult updateStatusInit() {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.updateStatusInit();
    }

    @UserSessionRequired
    @PostMapping("setCostFee")
    @ApiOperation(value = "设置费用", notes = "编辑状态")
    public SingleResult setCostFee(@RequestBody @Validated FirstVesselPriceReq req) {
        Long userId = getCurrentUserId();
        service.setCostFee(req,userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("setCostFeeInit")
    @ApiOperation(value = "设置费用初始化", notes = "编辑状态")
    public SingleResult setCostFeeInit(@RequestParam Long id) {
        return service.setCostFeeInit(id);
    }

    @UserSessionRequired
    @PostMapping("orderBack")
    @ApiOperation(value = "驳回", notes = "编辑状态")
    public SingleResult orderBack(@RequestBody @Validated FirstVesselBackReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.orderBack(req,userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("uploadImages")
    @ApiOperation(value = "上传图片（国内和境外）", notes = "编辑状态")
    public SingleResult orderBack(@RequestBody @Validated firstVesselUploadImagesReq req) {
        service.uploadImages(req);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("updateIsLab")
    @ApiOperation(value = "编辑贴标状态", notes = "编辑贴标状态")
    public SingleResult updateIsLab(@RequestBody @Validated FirstVesselUpdateLabellingReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.updateLabellingStatus(req);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("uploadCartonabel")
    @ApiOperation(value = "上传箱标", notes = "修改")
    public SingleResult uploadCartonabel(@RequestBody @Validated FirstVesselUploadFileReq req) {
        Long userId = getCurrentUserId();
        service.uploadCartonabel(req,userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("uploadDeclaration")
    @ApiOperation(value = "上传报关资料", notes = "修改")
    public SingleResult uploadDeclaration(@RequestBody @Validated FirstVesselUploadFileReq req) {
        Long userId = getCurrentUserId();
        service.uploadDeclaration(req,userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PutMapping("uploadCertificate")
    @ApiOperation(value = "上传报关凭证", notes = "修改")
    public SingleResult uploadCertificate(@RequestBody @Validated FirstVesselUploadFileReq req) {
        Long userId = getCurrentUserId();
        service.uploadCertificate(req,userId);
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("downloadDeclaration")
    @ApiOperation(value = "下载报关凭证", notes = "修改")
    public SingleResult downloadDeclaration(@RequestParam Long id) {
        return service.downloadDeclaration(id);
    }

    @UserSessionRequired
    @GetMapping("reDelivery")
    @ApiOperation(value = "再次派送", notes = "修改")
    public SingleResult reDelivery(@RequestParam Long id) {
        service.reDelivery(id,getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId));
    }

//    @GetMapping("/{id}")
//    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
//    public SingleResult detail(@PathVariable("id") Long id) {
//        return SingleResult.success("查询成功",service.detail(id));
//    }

//    @GetMapping("/pageInit")
//    @ApiOperation(value = "查询初始化", notes = "查询初始化")
//    public SingleResult pageInit() {
//        return service.pageInit();
//    }

//    @GetMapping("/list")
//    @ApiOperation(value = "查询", notes = "查询")
//    public SingleResult list(HttpServletRequest request){
//        Long companyId = getCompanyId();
//
//        Map<String, String> map = new HashMap<>();
//        String keyword = request.getParameter("keyword");
//        map.put("keyword", keyword);
//        return SingleResult.success("查询成功", service.getList(map));
//    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "endDate", value = "结束时间")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = FirstVesselOrderPageRes.class)})
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String status = request.getParameter("status");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("status", status);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        if(companyId != null){
            map.put("companyId", companyId+"");
        }

        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/FbaProductListPage")
    @ApiOperation(value = "fba商品列表（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult FbaProductListPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return fbaProductService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("fbaProduct/{id}")
    @ApiOperation(value = "fba商品详情", notes = "根据id查看详情")
    public SingleResult fbaProductDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", fbaProductService.detail(id));
    }

    @UserSessionRequired
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入入库单信息", notes = "导入入库单信息")
    public SingleResult uploadExcel(MultipartFile file) throws IOException {
        Long userId = getCurrentUserId();
        return SingleResult.success(service.importExcel(file));
    }


}

